package com.barunsw.bwf.docu_server.topology;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.barunsw.bwf.common.vo.ResultVo;
import com.barunsw.bwf.constants.Result;

@Controller
@RequestMapping(value = "/topology")
public class TopologyController {

	@Autowired
	private TopologyService topologyService;

	@RequestMapping(value = "/client", method = RequestMethod.GET)
	public @ResponseBody List<TopologyClientVo> getClient() throws Exception {
		List<TopologyClientVo> clientList = topologyService.getClientList();

		return clientList;
	}

	@RequestMapping(value = "/project", method = RequestMethod.GET)
	public @ResponseBody List<TopologyProjectVo> getProject() throws Exception {
		List<TopologyProjectVo> projectList = topologyService.getProjectList();

		return projectList;
	}

	@RequestMapping(value="/insertClient", method=RequestMethod.POST)
	public @ResponseBody ResultVo insertClient(
			@RequestBody TopologyClientVo topologyClientVo) throws Exception {

		ResultVo resultVo 			= new ResultVo(Result.OK, "");
		int checkClientCodeResult 	= topologyService.checkClientCode(topologyClientVo.getClientCode());
		int checkClientNameResult 	= topologyService.checkClientName(topologyClientVo.getClientName());
		
		if ( checkClientCodeResult > 0 && checkClientNameResult > 0 ) {
			resultVo.setResult(Result.CLIENT_INFO_EXIST);
			resultVo.setReason("이미 존재하는 고객사명, 고객사 코드 입니다.");
		}
		else if ( checkClientCodeResult > 0 ) {
			resultVo.setResult(Result.CLIENT_CODE_EXIST);
			resultVo.setReason("이미 존재하는 고객사 코드 입니다.");
		}
		else if ( checkClientNameResult > 0 ) {
			resultVo.setResult(Result.CLIENT_NAME_EXIST);
			resultVo.setReason("이미 존재하는 고객사명 입니다.");
		}
		else {
			int result = topologyService.insertClient(topologyClientVo);

			if ( result <= 0 ) {
				resultVo.setResult(Result.NOK);
				resultVo.setReason("고객사 추가 시 오류가 발생 하였습니다.");
			}
			
		}
		
		return resultVo;
	}

	@RequestMapping(value="/updateClient", method=RequestMethod.PUT)
	public @ResponseBody ResultVo updateClient(
			@RequestBody TopologyClientVo topologyClientVo) throws Exception {

		ResultVo resultVo 			= new ResultVo(Result.OK, "");
		int checkClientNameResult	= topologyService.checkClientName(topologyClientVo.getClientName());
		
		if ( checkClientNameResult > 0 ) {
			resultVo.setResult(Result.CLIENT_NAME_EXIST);
			resultVo.setReason("이미 존재하는 고객사명 입니다.");
		}
		else {
			int result = topologyService.updateClient(topologyClientVo);

			if ( result <= 0 ) {
				resultVo.setResult(Result.NOK);
				resultVo.setReason("고객사 수정 시 오류가 발생 하였습니다.");
			}
		}

		return resultVo;
	}

	@RequestMapping(value="/deleteClient/{clientCode}", method=RequestMethod.DELETE)
	public @ResponseBody ResultVo deleteClient(
			@RequestBody @PathVariable long clientCode) throws Exception {

		ResultVo resultVo 	= new ResultVo(Result.OK, "");
		int result 			= topologyService.deleteClient(clientCode);

		if ( result <= 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("고객사 삭제 시 오류가 발생 하였습니다.");
		}

		return resultVo;
	}

	@RequestMapping(value="/insertProject", method=RequestMethod.POST)
	public @ResponseBody ResultVo insertProject(
			@RequestBody TopologyProjectVo topologyProjectVo) throws Exception {

		ResultVo resultVo 			= new ResultVo(Result.OK, "");
		int checkProjectCodeResult 	= topologyService.checkProjectCode(topologyProjectVo);
		int checkProjectNameResult 	= topologyService.checkProjectName(topologyProjectVo);
		
		if ( checkProjectCodeResult > 0 && checkProjectNameResult > 0 ) {
			resultVo.setResult(Result.PROJECT_INFO_EXIST);
			resultVo.setReason("이미 존재하는 프로젝트명, 프로젝트 코드 입니다.");
		}
		else if ( checkProjectCodeResult > 0 ) {
			resultVo.setResult(Result.PROJECT_CODE_EXIST);
			resultVo.setReason("이미 존재하는 프로젝트 코드 입니다.");
		}
		else if ( checkProjectNameResult > 0 ) {
			resultVo.setResult(Result.PROJECT_NAME_EXIST);
			resultVo.setReason("이미 존재하는 프로젝트명 입니다.");
		}
		else {
			int result = topologyService.insertProject(topologyProjectVo);

			if ( result <= 0 ) {
				resultVo.setResult(Result.NOK);
				resultVo.setReason("프로젝트 추가 시 오류가 발생 하였습니다.");
			}
		}

		return resultVo;
	}

	@RequestMapping(value="/updateProject", method=RequestMethod.PUT)
	public @ResponseBody ResultVo updateProject(
			@RequestBody TopologyProjectVo topologyProjectVo) throws Exception {

		ResultVo resultVo 			= new ResultVo(Result.OK, "");
		int checkProjectNameResult 	= topologyService.checkProjectName(topologyProjectVo);
		
		if ( checkProjectNameResult > 0 ) {
			resultVo.setResult(Result.PROJECT_NAME_EXIST);
			resultVo.setReason("이미 존재하는 프로젝트명 입니다.");
		}
		else {
			int result = topologyService.updateProject(topologyProjectVo);

			if ( result <= 0 ) {
				resultVo.setResult(Result.NOK);
				resultVo.setReason("프로젝트 수정 시 오류가 발생 하였습니다.");
			}
		}

		return resultVo;
	}

	@RequestMapping(value="/deleteProject", method=RequestMethod.DELETE)
	public @ResponseBody ResultVo deleteProject(
			@RequestBody TopologyProjectVo topologyProjectVo) throws Exception {

		ResultVo resultVo 	= new ResultVo(Result.OK, "");
		int result 			= topologyService.deleteProject(topologyProjectVo);

		if ( result <= 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("프로젝트 삭제 시 오류가 발생 하였습니다.");
		}

		return resultVo;
	}
}
